package com.bss.utilities;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtilities {

	public static Timestamp convertToTimestamp(String Date)
{
		com.bss.framework.FrontController.trace("Date:"+Date);
		//the date type is mm/dd/yyyy hh:mm AM/PM
		String dd=(Date.substring(0,2));
		String mm=(Date.substring(3,5));
		String yyyy=(Date.substring(6,10));
		int hh=Integer.parseInt(Date.substring(11,13));
		String MM=(Date.substring(14,16));
		char amORpm=Date.charAt(17);

		if(amORpm=='P'&& hh<12)
		{
			hh+=12;
		}
		if(amORpm=='A' && hh==12)
		{
			hh=0;
		}
		Timestamp timestamp= Timestamp.valueOf(yyyy+"-"+mm+"-"+dd+" "+hh+":"+MM+":"+"00");
		com.bss.framework.FrontController.trace("timestamp:"+timestamp);
		return timestamp;

	}
	
	public static String printDate(String Date)
	{
		if(Date!=null)
		{
		String dd=(Date.substring(8,10));
		String mm=(Date.substring(5,7));
		String yyyy=(Date.substring(0,4));
		
		
		return dd+"/"+mm+"/"+yyyy;
		}
		else
		{
			return "";
		}
	}
	
	public static String dbDateToCalDate(String Date)
	{
		if(Date==null || Date.length()<16)
		{
			com.bss.framework.FrontController.trace("Date is Invalid");
			return null;
		}
		String ampm="AM";
		String dd=(Date.substring(8,10));
		String mm=(Date.substring(5,7));
		String yyyy=(Date.substring(0,4));
		int hh=Integer.parseInt(Date.substring(11,13));
		String MM=(Date.substring(14,16));
		
		if(hh>11)
		{
			if(hh==12)
			{
				
			}
			else
			{
				hh=hh-12;
			}
			ampm="PM";
		}
		
		if(hh>9)
		{
			return dd+"/"+mm+"/"+yyyy+" "+hh+":"+MM+" "+ampm;
		}
		else
		{
			return dd+"/"+mm+"/"+yyyy+" 0"+hh+":"+MM+" "+ampm;
		}
	}
	
	public static String incDbDateToCalDate(String Date)
	{
		String date="";
		String ampm="AM";
		String dd=(Date.substring(8,10));
		String mm=(Date.substring(5,7));
		String yyyy=(Date.substring(0,4));
		int hh=Integer.parseInt(Date.substring(11,13));
		String MM=(Date.substring(14,16));
	
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
    	java.util.Date indate = new java.util.Date();
    	
		Calendar cal1 = new GregorianCalendar();
		com.bss.framework.FrontController.trace(Integer.parseInt(mm));
		cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd),hh,Integer.parseInt(MM));
		
		cal1.add(Calendar.SECOND, (60*60*24));
		
		date=dateFormat.format(cal1.getTime());
		
		return date;
	}
	
	public static String inc7DbDateToCalDate(String Date)
	{
		String date="";
		String ampm="AM";
		String dd=(Date.substring(8,10));
		String mm=(Date.substring(5,7));
		String yyyy=(Date.substring(0,4));
		int hh=Integer.parseInt(Date.substring(11,13));
		String MM=(Date.substring(14,16));
	
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
    	java.util.Date indate = new java.util.Date();
    	
		Calendar cal1 = new GregorianCalendar();
		com.bss.framework.FrontController.trace(Integer.parseInt(mm));
		cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd),hh,Integer.parseInt(MM));
		
		cal1.add(Calendar.SECOND, (60*60*24*8));
		
		date=dateFormat.format(cal1.getTime());
		
		return date.substring(0,10);
	}
	
	public static String inc7DbDateToCalDateFirst(String Date)
	{
		String date="";
		String ampm="AM";
		String dd=(Date.substring(8,10));
		String mm=(Date.substring(5,7));
		String yyyy=(Date.substring(0,4));
		int hh=Integer.parseInt(Date.substring(11,13));
		String MM=(Date.substring(14,16));
	
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
    	java.util.Date indate = new java.util.Date();
    	
		Calendar cal1 = new GregorianCalendar();
		com.bss.framework.FrontController.trace(Integer.parseInt(mm));
		cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd)-1,hh,Integer.parseInt(MM));
		
		cal1.add(Calendar.SECOND, (60*60*24*8));
		
		date=dateFormat.format(cal1.getTime());
		
		return date.substring(0,10);
	}
	

	
	public static int findDateDiff(String Date1,String Date2)
	{
		if(Date1==null || Date1.length()<16)
		{
			com.bss.framework.FrontController.trace("Date is Invalid");
			return 0;
		}
		if(Date2==null || Date2.length()<16)
		{
			com.bss.framework.FrontController.trace("Date is Invalid");
			return 0;
		}
		int diff=0;
		int idd2;
		Calendar cal1 = new GregorianCalendar();
	    Calendar cal2 = new GregorianCalendar();
		String dd1=(Date1.substring(0,2));
		String mm1=(Date1.substring(3,5));
		String yyyy1=(Date1.substring(6,10));
		String hh1=Date1.substring(11,13);
		String MM1=(Date1.substring(14,16));
		char amORpm1=Date1.charAt(17);
		
		if(hh1.compareTo("12")==0 && amORpm1=='A')
		{
			hh1="00";
		}
		
		String dd2=(Date2.substring(0,2));
		String mm2=(Date2.substring(3,5));
		String yyyy2=(Date2.substring(6,10));
		String hh2=Date2.substring(11,13);
		String MM2=(Date2.substring(14,16));
		char amORpm2=Date2.charAt(17);
		
		if(hh2.compareTo("12")==0 && amORpm2=='A')
		{
			hh2="00";
		}
		
		if((Integer.parseInt(hh2)<8 || (Integer.parseInt(hh2)==12) )&& amORpm2=='A')
		{
			 idd2=Integer.parseInt(dd2)-1;
		}
		else
		{
			idd2=Integer.parseInt(dd2);
		}
		
		
		cal1.set(Integer.parseInt(yyyy1), Integer.parseInt(mm1)-1,Integer.parseInt(dd1)); 
	    cal2.set(Integer.parseInt(yyyy2), Integer.parseInt(mm2)-1,idd2);
		diff=daysBetween(cal1.getTime(),cal2.getTime());
		return diff;
	}
	
	
	public static int findNormalDateDiff(String Date1,String Date2)
	{
		if(Date1==null || Date1.length()<16)
		{
			com.bss.framework.FrontController.trace("Date is Invalid");
			return 0;
		}
		if(Date2==null || Date2.length()<16)
		{
			com.bss.framework.FrontController.trace("Date is Invalid");
			return 0;
		}
		
	    
		String dd1=(Date1.substring(0,2));
		String mm1=(Date1.substring(3,5));
		String yyyy1=(Date1.substring(6,10));
		String hh1=Date1.substring(11,13);
		String MM1=(Date1.substring(14,16));
		char amORpm1=Date1.charAt(17);
		
		if(hh1.compareTo("12")==0 && amORpm1=='A')
		{
			String.valueOf(Integer.valueOf(hh1) + 12); 
		}
		
		String dd2=(Date2.substring(0,2));
		String mm2=(Date2.substring(3,5));
		String yyyy2=(Date2.substring(6,10));
		String hh2=Date2.substring(11,13);
		String MM2=(Date2.substring(14,16));
		char amORpm2=Date2.charAt(17);
		
		if(hh2.compareTo("12")==0 && amORpm2=='A')
		{
			hh2 = String.valueOf(Integer.valueOf(hh2) + 12); 
		}
		
		// Calendar cal1 = new GregorianCalendar(Integer.parseInt(yyyy1), Integer.parseInt(mm1),Integer.parseInt(dd1), Integer.valueOf(hh1), Integer.valueOf(MM1)); 
	    // Calendar cal2 = new GregorianCalendar(Integer.parseInt(yyyy2), Integer.parseInt(mm2),Integer.parseInt(dd2), Integer.valueOf(hh2), Integer.valueOf(MM2));
		Calendar cal1 = new GregorianCalendar(Integer.parseInt(yyyy1), Integer.parseInt(mm1) - 1,Integer.parseInt(dd1)); 
	    Calendar cal2 = new GregorianCalendar(Integer.parseInt(yyyy2), Integer.parseInt(mm2) - 1,Integer.parseInt(dd2));
		
	    return daysBetween(cal1.getTime(),cal2.getTime());
		
	}
	
	 public static int daysBetween(Date d1, Date d2)
	 {
	     return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
	 }
	 
	 public static Date stripTimePortion(Date timestamp) {
		    long msInDay = 1000 * 60 * 60 * 24; // Number of milliseconds in a day
		    long msPortion = timestamp.getTime() % msInDay;
		    return new Date(timestamp.getTime() - msPortion);
		}

	 public static int noDaysDiffIn2Sets(String date1Set1,String date2Set1,String date1Set2,String date2Set2)
	 {
		int set2pos=0;
		String fromDate;
		String toDate;
		
		if(findDateDiff(date1Set2, date2Set1)<0)
		{
			return -1;
		}
		
		if(findDateDiff(date1Set2, date1Set1)>=0)
		{
			fromDate=date1Set1;
		}
		else
		{
			fromDate=date1Set2;
		}
		
		if(findDateDiff(date2Set1, date2Set2)>=0)
		{
			toDate=date2Set1;
		}
		else
		{
			toDate=date2Set2;
		}
		
		return findDateDiff(fromDate, toDate);
	 }
	 
	 public static String decCalDateBf8AM(String Date)
		{
		//the date type is mm/dd/yyyy hh:mm AM/PM
			String dd=(Date.substring(0,2));
			String mm=(Date.substring(3,5));
			String yyyy=(Date.substring(6,10));
			int hh=Integer.parseInt(Date.substring(11,13));
			String MM=(Date.substring(14,16));
			char amORpm=Date.charAt(17);
		
			if(hh<8 && amORpm=='A')
			{
			
				DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
				java.util.Date indate = new java.util.Date();
	    	
				Calendar cal1 = new GregorianCalendar();
				com.bss.framework.FrontController.trace(Integer.parseInt(mm));
				cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd)-1,23,59);
				
				return dateFormat.format(cal1.getTime());
			}
			
			return Date;
		}
		
	 public static String receiptFromDateFormat(String Date)
	 {
		 String date="";
			String ampm="AM";
			String dd=(Date.substring(8,10));
			String mm=(Date.substring(5,7));
			String yyyy=(Date.substring(0,4));
			int hh=Integer.parseInt(Date.substring(11,13));
			String MM=(Date.substring(14,16));
		
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
	    	java.util.Date indate = new java.util.Date();
	    	
			Calendar cal1 = new GregorianCalendar();
			com.bss.framework.FrontController.trace(Integer.parseInt(mm));
			
			if(hh >= 0 && hh < 8)
			{
				cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd)-1,8,0);
			}
			else
			{
				cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd),8,0);
			}
			
			
			
			date=dateFormat.format(cal1.getTime());
			
			return date;
			
			
	 }
	 
	 public static String receiptToDateFormat(String Date)
	 {
		 String date="";
			String ampm="AM";
			String dd=(Date.substring(8,10));
			String mm=(Date.substring(5,7));
			String yyyy=(Date.substring(0,4));
			int hh=Integer.parseInt(Date.substring(11,13));
			String MM=(Date.substring(14,16));
		
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm aaa");
	    	java.util.Date indate = new java.util.Date();
	    	
			Calendar cal1 = new GregorianCalendar();
			com.bss.framework.FrontController.trace(Integer.parseInt(mm));
			if(hh >=8 && hh <= 23)
			{
				cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd)+1,7,59);
			}
			else
			{
				cal1.set(Integer.parseInt(yyyy),Integer.parseInt(mm)-1,Integer.parseInt(dd),7,59);
			}
			
			
			date=dateFormat.format(cal1.getTime());
			
			return date;
	 }
	 
	 
}
